package org.fu.crowd.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.fu.crowd.entity.Role;
import org.fu.crowd.entity.RoleExample;
import org.fu.crowd.mapper.RoleMapper;
import org.fu.crowd.service.api.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;




    /**
     *  查询页面数据
     * @param pageNum 页号
     * @param pageSize 每页显示数据的行数
     * @param keyword 关键字 默认为空
     * @return
     */
    @Override
    public PageInfo<Role> getPageInfo(Integer pageNum, Integer pageSize, String keyword) {

        //条件拼接
        RoleExample roleExample = new RoleExample();
        roleExample.or().andNameLike("%"+keyword+"%");
        //开启分页查询
        PageHelper.startPage(pageNum,pageSize);
        List<Role> roles = roleMapper.selectByExample(roleExample);
        PageInfo<Role> pageInfo = new PageInfo<>(roles,5);
        return pageInfo;
    }





    /**
     * 保存role信息
     * @param role
     */
    @Override
    public void saveRole(Role role) {
        roleMapper.insert(role);
    }








    /**
     * 修改信息
     * @param role
     */
    @Override
    public void updateRole(Role role) {
        roleMapper.updateByPrimaryKey(role);
    }








    /**
     * 删除结点
     * @param list
     */
    @Override
    public void removeRole(List<Integer> list) {
        RoleExample roleExample = new RoleExample();
        roleExample.createCriteria().andIdIn(list);
        roleMapper.deleteByExample(roleExample);
    }






    /**
     * 获取已分配结点
     * @param adminId
     */
    @Override
    public List<Role> getAssignRole(Integer adminId) {

        return roleMapper.selectAssignedRole(adminId);
    }





    /**
     * 获取未分配结点
     * @param adminId
     */
    @Override
    public List<Role> getUnAssignRole(Integer adminId) {
        return roleMapper.selectUnAssignedRole(adminId);
    }





    /**
     * 保存重新分配后的的角色
     * @return
     */
    @Override
    public void saveRelationship(Integer adminId, List<Integer> roleIdList) {
        //先删除旧的
        roleMapper.deleteRelationship(adminId);
        //保存新的
        if (roleIdList!=null && roleIdList.size()>0){
            roleMapper.insertNewRelationship(adminId,roleIdList);
        }
    }
}
